package cn.javaxc.mvcwork.util.io;
import java.io.*;
import java.util.*;


public class NCFTPApp {

  private String appExecutable = "ncftp";
  private int result = 0;
  private ArrayList files = new ArrayList();
  private String stdOut = null;
  private String stdErr = null;
  private boolean deleteSourceFilesAfterSend = false;
  private boolean makeRemoteDir = false;


  
  public NCFTPApp() {
  }


  
  public void setAppExecutable(String tmp) {
    this.appExecutable = tmp;
  }


  
  public void setResult(int tmp) {
    this.result = tmp;
  }


  
  public void setFiles(ArrayList tmp) {
    this.files = tmp;
  }


  
  public void setStdOut(String tmp) {
    this.stdOut = tmp;
  }


  
  public void setStdErr(String tmp) {
    this.stdErr = tmp;
  }


  
  public void setDeleteSourceFilesAfterSend(boolean tmp) {
    this.deleteSourceFilesAfterSend = tmp;
  }


  
  public void setMakeRemoteDir(boolean tmp) {
    this.makeRemoteDir = tmp;
  }


  
  public String getAppExecutable() {
    return appExecutable;
  }


  
  public int getResult() {
    return result;
  }


  
  public ArrayList getFiles() {
    return files;
  }


  
  public String getStdOut() {
    return stdOut;
  }


  
  public String getStdErr() {
    return stdErr;
  }


  
  public boolean getDeleteSourceFilesAfterSend() {
    return deleteSourceFilesAfterSend;
  }


  
  public void addFile(String tmp) {
    files.add(tmp);
  }


  
  public int put(String ftpUrl) {
    stdOut = null;
    stdErr = null;
    StringBuffer out = new StringBuffer();
    StringBuffer err = new StringBuffer();

    try {
      ArrayList command = new ArrayList();
      command.add(appExecutable + "put");
      if (this.getFtpUser(ftpUrl) != null) {
        command.add("-u");
        command.add(this.getFtpUser(ftpUrl));
      }
      if (this.getFtpPassword(ftpUrl) != null) {
        command.add("-p");
        command.add(this.getFtpPassword(ftpUrl));
      }
      //Do not try to resume
      command.add("-Z");
      //Upload as a tmp file, then rename
      command.add("-S");
      command.add(".tmp");
      //Make sure the remote directory exists, or create it
      if (makeRemoteDir) {
        command.add("-m");
      }
      //Delete the source files when done
      if (deleteSourceFilesAfterSend) {
        command.add("-DD");
      }
      //Set the host
      command.add(this.getFtpHost(ftpUrl));
      //Set the remote dir
      command.add(this.getFtpRemoteDir(ftpUrl));
      //Add the files
      Iterator fileList = files.iterator();
      while (fileList.hasNext()) {
        String file = (String) fileList.next();
        command.add(file);
      }

      String[] commands = (String[]) command.toArray(new String[0]);
      Process process = Runtime.getRuntime().exec(commands);
      BufferedReader brErr
          = new BufferedReader(
              new InputStreamReader(process.getErrorStream()));
      /*
       *  BufferedReader brOut
       *  = new BufferedReader(
       *  new InputStreamReader(process.getOutputStream()));
       */
      String tmp = null;
      while ((tmp = brErr.readLine()) != null) {
        err.append(tmp);
      }
      stdErr = err.toString();
      /*
       *  while ((tmp = brOut.readLine()) != null) {
       *  out.append(tmp);
       *  }
       */
      process.waitFor();
      result = process.exitValue();
    } catch (Exception e) {
      result = -1;
      e.printStackTrace(System.out);
    }

    return result;
  }


  
  private static String getFtpUser(String ftpUrl) {
    String tmp = ftpUrl.substring(
        ftpUrl.indexOf("ftp://") + 6, ftpUrl.indexOf("@"));
    if (tmp.indexOf(":") > -1) {
      return tmp.substring(0, tmp.indexOf(":"));
    } else {
      return tmp;
    }
  }


  
  private static String getFtpPassword(String ftpUrl) {
    String tmp = ftpUrl.substring(
        ftpUrl.indexOf("ftp://") + 6, ftpUrl.indexOf("@"));
    if (tmp.indexOf(":") > -1) {
      return tmp.substring(tmp.indexOf(":") + 1);
    } else {
      return null;
    }
  }


  
  private static String getFtpHost(String ftpUrl) {
    String tmp = ftpUrl.substring(ftpUrl.indexOf("@") + 1);
    if (tmp.indexOf(":") > -1) {
      return tmp.substring(0, tmp.indexOf(":"));
    } else {
      return tmp;
    }
  }


  
  private static String getFtpRemoteDir(String ftpUrl) {
    String tmp = ftpUrl.substring(ftpUrl.lastIndexOf(":") + 1);
    if (tmp.indexOf("@") > -1) {
      return "./";
    } else {
      return tmp;
    }
  }
}
